package com.AUI.components
{
	import com.AUI.*;
	import com.AUI.events.*;
	
	import flash.display.*;
	import flash.events.*;
	import flash.geom.*;
	import flash.text.*;
	import flash.utils.*;
	/**
	 * Button List.
	 * @author HuangMinPing
	 * @site www.AminLab.com
	 */
	public class DropDownList extends ToggleButton
	{
		/**
		 * [element] list.
		 */
		public var e_list:List;
		/**
		 * Constructor.
		 */
		public function DropDownList(items:Array = null)
		{
			// init elements
			AUIPrivate.addPreSelectorNames(this.ename +' > list');
			e_list = new List;
			// init attributes
			depth = 4;
			if(items){ value = items; }
			// init events
			e_list.addEventListener(AItemEvent.CLICK,onListItemClick,false,0,true);
			
			/**************** INITCOMPLETE ****************/
			Object(this).constructor == DropDownList && initComplete();
		}
		/**
		 * The number of items in this component.
		 */
		public function get numItems():int
		{
			return e_list.numElements;
		}
		/**
		 * Removes a option(AButton) from ButtonList by name.
		 * @param name Option name.
		 * @return The option that was removed.
		 */
		public function removeItem(name:String):Button
		{
			return removeItemAt(e_list.getItemIndex(name));
		}
		/**
		 * Removes a option(AButton) from ButtonList by name.
		 * @param name Option name.
		 * @return The option that was removed.
		 */
		public function removeItemAt(index:int):Button
		{
			var option:Button = e_list.removeItemAt(index);
			if(selectedIndex == -1)
			{
				super.value = "";
			}
			return option;
		}
		/**
		 * Replaces the option(AButton) at the specified index location with another option.
		 * @param name Option name.
		 * @param index The index position of the option to be replaced.
		 * @return The option that was replaced.
		 */
		public function replaceItemAt(name:String,index:int):DisplayObject
		{
			var removeOption:Button = e_list.replaceItemAt(name,index);
			if(index == selectedIndex)
			{
				super.value = e_list.getItemAt(index);
			}
			return removeOption;
		}
		/**
		 * Retrieves the name of the option(AButton) at the specified index.
		 * @param index The index position of the option to be retrieved.
		 */
		public function getItemAt(index:int):String
		{
			return e_list.getItemAt(index);
		}
		/**
		 * Retrieves the index of the option(AButton) by name.
		 * @param index The index position of the option to be retrieved.
		 */
		public function getItemIndex(name:String):int
		{
			return e_list.getItemIndex(name);
		}
		/**
		 * Appends a option(AButton) to the end of the ButtonList.
		 * @param name Option name.
		 * @return The option that was added.
		 */
		public function addItem(name:String):Button
		{
			return addItemAt(name,this.numItems);
		}
		/**
		 * Inserts a option(AButton) into the list at the specified index location.
		 * @param name Option name.
		 * @param index The index at which to add the option.
		 * @return The option to be added to the list.
		 */
		public function addItemAt(name:String,index:int):Button
		{
			var option:Button = e_list.addItemAt(name,index);
			if(selectedIndex == -1)
			{
				selectedIndex = 0;
			}
			return option;
		}
		/**
		 * Get or set a string array that indicates the name of items of the ButtonList.
		 */
		override public function set value(variable:*):void
		{
			e_list.value = variable;
			super.value  = selectedIndex == -1 ? '' : e_list.getItemAt(selectedIndex);
			updateContentBox();
		}
		override public function get value():*
		{
			return e_list.value;
		}
		/**
		 * Get or set a integer array that representing the currently selected indices.The value "null" be gotten or set is indicates nothing selected.
		 * <p>In single selection mode,the return array length is always 1 when be gotten,and it use first value of the given array when be set.</p>
		 * <p>Note: You can use getItemAt function to get the selected items.</p>
		 */
		public function set selectedIndex(value:int):void
		{
			e_list.selectedIndex = value;
			super.value = (value == -1 ? '' : e_list.getItemAt(value));
		}
		public function get selectedIndex():int
		{
			return e_list.selectedIndex;
		}
		/**
		 * @private
		 */
		override protected function onClick(e:MouseEvent):void
		{
			super.onClick(e);
			if(e_list.parent)
			{
				stage.removeEventListener(MouseEvent.CLICK,onCloseList);
				stage.removeChild(e_list);
			}
			else
			{
				var p:Point = this.localToGlobal(new Point(this.boxModel.border.x,this.boxModel.border.y));
				e_list.width = this.boxModel.border.width;
				e_list.x = p.x;
				if(p.y + this.boxModel.border.height + e_list.height > stage.stageHeight)
				{
					e_list.y = p.y - e_list.height;
				}
				else
				{
					e_list.y = p.y + this.boxModel.border.height;
				}
				e.stopPropagation();
				stage.addChild(e_list);
				stage.addEventListener(MouseEvent.CLICK,onCloseList,false,0,true);
			}
		}
		/**
		 * Called when MouseEvent.CLICK event has been dispatched on stage.
		 */
		protected function onCloseList(e:MouseEvent):void
		{
			e_list.stage.removeEventListener(MouseEvent.CLICK,onCloseList);
			e_list.stage.removeChild(e_list);
			this.checked = false;
		}
		/**
		 * Called when MouseEvent.CLICK event has been dispatched on stage.
		 */
		protected function onListItemClick(e:AItemEvent):void
		{
			super.value = e_list.getItemAt(selectedIndex);
		}
	}
}